/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Istitucion.java
 *
 * Created on 14-ene-2013, 8:20:44
 */
package sisvoto.vista;

import sisvoto.DAO.ConsultasBD;
import sisvoto.ModeloDAO.InstitucionDAO;
import sisvoto.Utiles.Mensajes;
import sisvoto.modelo.Institucion;

/**
 *
 * @author DJ2-PC
 */
public class Istitucion extends javax.swing.JDialog {

    private Institucion institucion;

    /** Creates new form Istitucion */
    public Istitucion(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

        if (new ConsultasBD().buscarTodos("Institucion").isEmpty()) {
            this.institucion = new Institucion();
        } else {
            this.institucion = (Institucion) new ConsultasBD().buscarTodos("Institucion").get(0);
        }
        initComponents();
    }

    public Institucion getInstitucion() {
        return institucion;
    }

    public void setInstitucion(Institucion institucion) {
        this.institucion = institucion;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        imagePanel1 = new com.mycsistemas.swingec.panel.ImagePanel();
        curvesPanel1 = new com.mycsistemas.swingec.panel.CurvesPanel();
        jLblInstitucion = new javax.swing.JLabel();
        jPanelInstitucion = new javax.swing.JPanel();
        jTxtRepresentante = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTxtTelefono = new javax.swing.JTextField();
        jTxtDireccion = new javax.swing.JTextField();
        jTxtNombre = new javax.swing.JTextField();
        jLblNombre = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jBtnCancelar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Institución");

        imagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/''_10102012_1738.jpeg"))); // NOI18N

        curvesPanel1.setLayout(null);

        jLblInstitucion.setFont(new java.awt.Font("Georgia", 3, 18));
        jLblInstitucion.setForeground(new java.awt.Color(255, 255, 255));
        jLblInstitucion.setText("Institución");
        curvesPanel1.add(jLblInstitucion);
        jLblInstitucion.setBounds(160, 20, 120, 30);

        jPanelInstitucion.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Institución", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 10), java.awt.Color.white)); // NOI18N
        jPanelInstitucion.setForeground(new java.awt.Color(255, 255, 255));
        jPanelInstitucion.setOpaque(false);
        jPanelInstitucion.setLayout(null);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${institucion.representante}"), jTxtRepresentante, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jPanelInstitucion.add(jTxtRepresentante);
        jTxtRepresentante.setBounds(140, 150, 230, 30);

        jLabel5.setFont(new java.awt.Font("Elephant", 0, 14));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Representante:");
        jPanelInstitucion.add(jLabel5);
        jLabel5.setBounds(20, 150, 120, 30);

        jLabel4.setFont(new java.awt.Font("Elephant", 0, 14));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Teléfono:");
        jPanelInstitucion.add(jLabel4);
        jLabel4.setBounds(20, 110, 70, 30);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${institucion.telefono}"), jTxtTelefono, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jPanelInstitucion.add(jTxtTelefono);
        jTxtTelefono.setBounds(100, 110, 270, 30);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${institucion.direccion}"), jTxtDireccion, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jPanelInstitucion.add(jTxtDireccion);
        jTxtDireccion.setBounds(100, 70, 270, 30);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${institucion.nombre}"), jTxtNombre, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jPanelInstitucion.add(jTxtNombre);
        jTxtNombre.setBounds(100, 30, 270, 30);

        jLblNombre.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblNombre.setForeground(new java.awt.Color(255, 255, 255));
        jLblNombre.setText("Nombre: ");
        jPanelInstitucion.add(jLblNombre);
        jLblNombre.setBounds(20, 30, 80, 30);

        jLabel3.setFont(new java.awt.Font("Elephant", 0, 14));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Dirección:");
        jPanelInstitucion.add(jLabel3);
        jLabel3.setBounds(20, 70, 80, 30);

        curvesPanel1.add(jPanelInstitucion);
        jPanelInstitucion.setBounds(40, 60, 390, 200);

        jBtnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/DeleteRed.png"))); // NOI18N
        jBtnCancelar.setText("Salir");
        jBtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelarActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnCancelar);
        jBtnCancelar.setBounds(260, 270, 120, 41);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/save_32.png"))); // NOI18N
        jButton1.setText("Guardar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        curvesPanel1.add(jButton1);
        jButton1.setBounds(130, 270, 120, 41);

        javax.swing.GroupLayout imagePanel1Layout = new javax.swing.GroupLayout(imagePanel1);
        imagePanel1.setLayout(imagePanel1Layout);
        imagePanel1Layout.setHorizontalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE)
        );
        imagePanel1Layout.setVerticalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    if (validarCampos()) {
        InstitucionDAO ins;
        if (institucion.getId() == null) {
            ins = new InstitucionDAO(institucion);
            try {
                ins.persist();
                new Mensajes().StandarTick(this, "Almacenado Exitosamente");
            } catch (Exception ex) {
                new Mensajes().StandarWarning(this, "No Se Pudo Almacenar");
            }
        } else {
            ins = new InstitucionDAO(institucion);
            try {
                ins.update();
                new Mensajes().StandarTick(this, "Actualizado Exitosamente");
            } catch (Exception ex) {
                new Mensajes().StandarWarning(this, "No Se Pudo Actualizar");
            }
        }

    } else {
        new Mensajes().StandarWarning(this, "Ingrese Todos los Campos");
    }
}//GEN-LAST:event_jButton1ActionPerformed

private void jBtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelarActionPerformed
// TODO add your handling code here:
    dispose();
}//GEN-LAST:event_jBtnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.mycsistemas.swingec.panel.CurvesPanel curvesPanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel1;
    private javax.swing.JButton jBtnCancelar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLblInstitucion;
    private javax.swing.JLabel jLblNombre;
    private javax.swing.JPanel jPanelInstitucion;
    private javax.swing.JTextField jTxtDireccion;
    private javax.swing.JTextField jTxtNombre;
    private javax.swing.JTextField jTxtRepresentante;
    private javax.swing.JTextField jTxtTelefono;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private boolean validarCampos() {
        if (!jTxtDireccion.getText().isEmpty()) {
            if (!jTxtNombre.getText().isEmpty()) {
                if (!jTxtRepresentante.getText().isEmpty()) {
                    if (!jTxtTelefono.getText().isEmpty()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
